use utf8;

my $stats = $blocnote->add('stats', -label => 'Statistiques',);
$stats->configure( -background => 'white',);

gestion_bd::createView();

############################################################################################
#                           Définition de l'onglet Statistiques                            #
############################################################################################

my $blocnoteStat = $stats->NoteBook(-backpagecolor => 'white', -inactivebackground => 'grey')->pack( qw/-fill both -expand 1 -padx 10 -pady 10 -ipady 10/);

my $game = $blocnoteStat->add('games', -label => 'Jeux',);
$game->configure( -background => 'white',);

my $event = $blocnoteStat->add('events', -label => 'Evenements',);
$event->configure( -background => 'white',);

my $book = $blocnoteStat->add('books', -label => 'Livres',);
$book->configure( -background => 'white',);

############################################################################################
#                                   Statistiques des Jeux                                  #
############################################################################################

#Mise en place du titre
my $c = $game->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1); 
$game->Label ( -text => "Classement des jeux selon le nombre cumulé de joueurs", -background => 'white')->pack();
$c = $game->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1); 

my $field = $game->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

my $topGames = gestion_bd::statGame();
my $i = 1;

$field->Label(-text => 'Nom du Jeu', -relief =>'groove',)->grid(-row => 0, -column => 0, -sticky => 'ew');
$field->Label(-text => 'Nombre de Total de Joueurs', -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => 'ew');

foreach my $g (sort {-$topGames->{$a}{participants} <=> -$topGames->{$b}{participants}} keys %{$topGames}) {
	$field->Label(-text => $topGames->{$g}{nomJeu}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 0, -sticky => 'ew');
	$field->Label(-text => $topGames->{$g}{participants}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 1, -sticky => 'ew');
	$i = $i + 1;
}

############################################################################################
#                                Statistiques des Evenements                               #
############################################################################################

$c = $event->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1); 
$event->Label ( -text => "Nombre d'événements auxquel un adhérent a participé depuis le début de l'année", -background => 'white')->pack();
$c = $event->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1); 

$field = $event->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

my $topEvents = gestion_bd::statEvent();
$i = 0;

$field->Label(-text => 'Nom', -relief =>'groove',)->grid(-row => $i, -column => 0, -sticky => 'ew');
$field->Label(-text => 'Prénom', -relief =>'groove',)->grid(-row => $i, -column => 1, -sticky => 'ew');
$field->Label(-text => 'nombre de Participations', -relief =>'groove',)->grid(-row => $i, -column => 2, -sticky => 'ew');

$i = $i + 1;

foreach my $e (sort {-$topEvents->{$a}{participations} <=> -$topEvents->{$b}{participations}} keys %{$topEvents}) {
	$field->Label(-text => $topEvents->{$e}{nomPersonne}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 0, -sticky => 'ew');
	$field->Label(-text => $topEvents->{$e}{prenomPersonne}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 1, -sticky => 'ew');
	$field->Label(-text => $topEvents->{$e}{participations}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 2, -sticky => 'ew');
	$i = $i + 1;
}

############################################################################################
#                                  Statistiques des Livres                                 #
############################################################################################

#----------------- Moyenne des livres empruntés durant un mois sur un an ------------------#

$c = $book->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1); 
$book->Label ( -text => "Moyenne annuelle du nombre mensuel d'emprunts des livres", -background => 'white')->pack();
$c = $book->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1); 

$field = $book->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

my $topBooksYear = gestion_bd::statBookYear();
$i = 0;

$field->Label(-text => 'Titre', -relief =>'groove',)->grid(-row => 0, -column => 0, -sticky => 'ew');
$field->Label(-text => 'Moyenne d\'emprunts', -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => 'ew');

$i = $i + 1;

foreach my $b (sort {-$topBooksYear->{$a}{moyenne} <=> -$topBooksYear->{$b}{moyenne}} keys %{$topBooksYear}) {
	$field->Label(-text => $topBooksYear->{$b}{titre}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 0, -sticky => 'ew');
	$field->Label(-text => $topBooksYear->{$b}{moyenne}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 1, -sticky => 'ew');
	$i = $i + 1;
}

#----------------------- Classement des livres les plus empruntés ------------------------#

$c = $book->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack(-pady => 2);
$c->createLine(1,1, 500,1); 
$book->Label ( -text => "Classement des livres les plus empruntés", -background => 'white')->pack();
$c = $book->Canvas(-width => 500, -height => 1, -background => 'white', -borderwidth => 0)->pack();
$c->createLine(1,1, 500,1); 

$field = $book->Frame(-background => 'white')->pack(-anchor => 'n', -pady => 15, -padx => 15);

my $topBooks = gestion_bd::statBook();
$i = 0;

$field->Label(-text => 'Titre', -relief =>'groove',)->grid(-row => 0, -column => 0, -sticky => 'ew');
$field->Label(-text => 'Nombre d\'emprunts', -relief =>'groove',)->grid(-row => 0, -column => 1, -sticky => 'ew');

$i = $i + 1;

foreach my $b (sort {-$topBooks->{$a}{nbEmprunt} <=> -$topBooks->{$b}{nbEmprunt}} keys %{$topBooks}) {
	$field->Label(-text => $topBooks->{$b}{titre}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 0, -sticky => 'ew');
	$field->Label(-text => $topBooks->{$b}{nbEmprunt}, -background => 'white', -relief =>'groove')->grid(-row => $i, -column => 1, -sticky => 'ew');
	$i = $i + 1;
}


1;
